import re


class GradeBook:
    """
    定义成绩单类，可以添加学生类，也可以添加课程类
    """

    def __init__(self):
        self._gradebook_dict = {}

    def add_student(self, student):
        self._gradebook_dict[student.name] = student._student_gradebook_dict

    def _add_grade_forGradeBook(self, name, subject):
        if name in self._gradebook_dict:
            self._gradebook_dict[name][subject.subname] = subject.grade
            print("完成课程成绩增加！")
        else:
            print("没有这个哥们，请重新建档！")

    def average(self, name):
        sum = 0
        if name in self._gradebook_dict:
            for _, v in self._gradebook_dict[name].items():
                sum += int(v)
            return '%.2f' % (sum / len(self._gradebook_dict[name]))
        else:
            print("没有这个哥们，请重新建档！")

    def show_one(self, name):
        for k, v in self._gradebook_dict[name].items():
            print('{}:{}'.format(k, v))

    def get_gradebook_dict(self):
        for k, v in self._gradebook_dict.items():
            print('{}:{}'.format(k, v))
        # print(self._gradebook_dict)


class Student:
    """
    定义学生类，可以添加课程类
    """

    def __init__(self, name):
        self.name = name
        self._student_gradebook_dict = {}

    def add_subject(self, subject):
        self._student_gradebook_dict[subject.subname] = subject.grade

    def get_student_gradebook_dict(self):
        print(self._student_gradebook_dict)


class Subject:
    """
    定义课程类，包含课程名称和成绩
    """

    def __init__(self, subname, grade):
        self.subname = subname
        self.grade = grade


def addin(gradebook, student_list: list):
    # 负责将输入的名字成绩进行处理，并传入实例gradebook中，student_list
    student_list = re.split('\W+', student_list)
    student_one = Student(student_list[0])
    for j in range(1, len(student_list), 2):
        if (j + 2) <= len(student_list):
            # 创建课程对象
            subject_one = Subject(student_list[j], student_list[j + 1])
            # 将课程对象加入学生对象
            student_one.add_subject(subject_one)
        # 每完成对一个学生对象的信息输入，都将该对象加入gradebook对象中
        gradebook.add_student(student_one)
    print("建档成功！")


def add_grade(gradebook, info):
    # ["王尼美；历史:85"]
    info = re.split('\W+', info)
    subject_one = Subject(info[1], info[2])
    gradebook._add_grade_forGradeBook(info[0], subject_one)


def main():
    gradebook = GradeBook()
    flag = ""
    while flag != "exit":
        print("请输入选项编码\n"
              "1:为学生建档\n"
              "2:查看某学生所有成绩\n"
              "3:为已有学生增加课程成绩\n"
              "4:查看某学生平均成绩\n"
              "5:查看系统中所有学生信息\n"
              "exit:退出")

        flag = input(">>>")
        if flag == "1":
            print("请输入学生档案，例如：王尼美；语文：93，数学：98，英语：100。")
            student = input(">>>")
            addin(gradebook, student)
            continue
        # student = ["王尼玛；语文：100，数学：95", "王尼美；语文：93，数学：98，英语：100。",
        #            "张全蛋；语文：63，数学：93，物理：90。", "铁锤；语文：74，数学：83，英语：67，化学：61。"]
        # student = "王尼玛；语文：100，数学：95，英语：93。"
        # student = "王尼玛 语文 100 数学 95 英语 93"

        # for i in range(len(student)):
        #     addin(gradebook, student[i])
        if flag == "2":
            print("请输入学生姓名。例如：王尼美")
            name_one = input(">>>")
            gradebook.show_one(name_one)


        if flag == "3":
            print("请输入学生姓名及课程成绩。例如：王尼美；历史：85。")
            addinfo = input(">>>")
            add_grade(gradebook, addinfo)

            # add_grade(gradebook, "蛋蛋", addinfo)

        if flag == "4":
            print("请输入学生姓名。例如：王尼美")
            average_name = input(">>>")
            rst = gradebook.average(average_name)
            print('{}的平均分是{}'.format(average_name, rst))

        if flag == "5":
            gradebook.get_gradebook_dict()


if __name__ == '__main__':
    main()
